package socketVersion.Backend.Flights;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Set;



public class FlightServerImplementation implements FlightInterface{

	private Hashtable<Integer,Flight> flights;
	private Hashtable<Integer,LinkedList<Integer>> reservations;
	
	public FlightServerImplementation(){
		flights = new Hashtable<Integer, Flight>();
		reservations = new Hashtable<Integer,LinkedList<Integer>>();
	}
	
	
	@Override
	public boolean addFlight(int id, int flightNum, int flightSeats,int flightPrice) {
		System.out.println("Adding flight: ("+id+","+flightNum+","+flightSeats+","+flightPrice+")");
		if(flightSeats <0 || flightPrice <=0){
			return false;
		}
		
		Flight f = flights.get(flightNum);
		if(f!=null){
			flights.remove(flightNum);
			flights.put(flightNum, new Flight(flightNum,f.getSeats() + flightSeats,flightPrice));
		}
		else{
			flights.put(flightNum, new Flight(flightNum,flightSeats,flightPrice));
		}
		return true;
	}

	@Override
	public boolean deleteFlight(int id, int flightNum) {
		System.out.println("Deleting flight: ("+id+","+flightNum+")");
		LinkedList<Integer> customers = new LinkedList<Integer>();
		customers = reservations.get(flightNum);
		if(customers == null){
			flights.remove(flightNum);
			return true;
		}
		
		if(customers.size()!=0){
			return false;
		}
		else{
			flights.remove(flightNum);
			return true;
		}
	}

	@Override
	public int queryFlight(int id, int flightNumber) {
		System.out.println("Querying flight: ("+id+","+flightNumber+")");
		Flight f = flights.get(flightNumber);
		if(f==null){
			return 0;
		}
		int totalSeats = f.getSeats();
		LinkedList<Integer> customers = reservations.get(flightNumber);
		if(customers==null){
			return totalSeats;
		}
		else{
			return totalSeats - customers.size();
		}
	}

	@Override
	public int queryFlightPrice(int id, int flightNumber) {
		System.out.println("Querying  flight price: ("+id+","+flightNumber+")");
		Flight f = flights.get(flightNumber);
		if(f==null){
			return 0;
		}
		else{
			return f.getPrice();
		}
	}

	@Override
	public boolean reserveFlight(int id, int customer, int flightNumber) {
		System.out.println("Reserving flight: ("+id+","+customer+","+flightNumber+")");
		Flight f = flights.get(flightNumber);
		if(f==null){
			System.out.println("Flight: " + flightNumber + "does not exist");
			return false;
		}
		else{
			if(queryFlight(id,flightNumber)>0){
				LinkedList<Integer> customers = reservations.get(flightNumber);
				if(customers==null){
					customers = new LinkedList<Integer>();
					customers.add(customer);
					reservations.put(flightNumber, customers);
				}
				else{
					customers.add(customer);
				}
				return true;
			}
			else{
				return false;
			}
		}
	}


	@Override
	public boolean deleteReservation(int id, int customer) {
		System.out.println("Deleting reservations: ("+id+","+customer+")");
		Set<Integer> keys = reservations.keySet();
		Object[] keyArray = keys.toArray();
		for(int i = 0;i<keyArray.length;i++){
			LinkedList<Integer> customers = reservations.get((Integer)keyArray[i]);
			while(customers.contains((Integer)customer)){
				customers.remove((Integer)customer);
			}
		}
		
		return true;
	}


	@Override
	public int calculateBill(int id, int customer) {
		System.out.println("Calculating bill: ("+id+","+customer+")");
		Set<Integer> keys = reservations.keySet();
		Object[] keyArray = keys.toArray();
		int totalBill = 0;
		
		for(int i = 0;i<keyArray.length;i++){
			LinkedList<Integer> customers = reservations.get((Integer)keyArray[i]);
			int noRooms = 0;
			for(int j = 0;j<customers.size();j++){
				if(customer == (int)customers.get(j)){
					noRooms++;
				}
			}
			totalBill+= (noRooms*queryFlightPrice(-1,(Integer)keyArray[i]));
		}
		
		return totalBill;
	}

}
